package de.rj.urwalking.ui.fragments.dialog;

import java.util.ArrayList;





import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.actionbarsherlock.app.SherlockDialogFragment;

import de.rj.urwalking.R;
import de.rj.urwalking.config.Config;
import de.rj.urwalking.config.Constants;
import de.rj.urwalking.data.survey.CreateParticipantSurvey;
import de.rj.urwalking.data.survey.HeaderQuestion;
import de.rj.urwalking.data.survey.Question;
import de.rj.urwalking.data.survey.Question.OnAnswerChangedListener;
import de.rj.urwalking.ui.fragments.QuestionsFragment;

/**
 * This Fragment displays the Survey that has to be taken when registering a new user/participant.
 * 
 * @author rjj
 * 
 */
public class RegistrationSurveyFragment extends SherlockDialogFragment implements OnAnswerChangedListener {

	private CreateParticipantSurvey mRegistrationSurvey;
	private Button mFinishSurveyButton;

	/**
	 * The enclosing activity has to implement this interface in order to get notified when the survey is complete.
	 */
	public interface RegistrationSurveyListener {
		void onFinishRegistrationSurvey(CreateParticipantSurvey survey);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		View root = initUI(inflater, container);
		WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
	    wmlp.gravity = Gravity.LEFT;
	    
		// Get the questions
		@SuppressWarnings("unchecked")
		ArrayList<Question> questions = (ArrayList<Question>) getArguments().getSerializable("questions");

		// Create the survey
		mRegistrationSurvey = new CreateParticipantSurvey(questions);
		mRegistrationSurvey.setTimestamp(System.currentTimeMillis());

		// Register this fragment as listener for the answers
		mRegistrationSurvey.setCallbackForQuestions(this);

		// Build another fragment inside this one which actually contains the
		// questions. This way, we can reuse the
		// QuestionsFragment, which is also used inside QuestionsActivity.
		Fragment fragment = createNestedFragment(mRegistrationSurvey.getCurrentQuestions());
		showNestedFragment(fragment);

		return root;

	}

	private View initUI(LayoutInflater inflater, ViewGroup container) {

		getDialog().setTitle(R.string.registration_successful);

		View root = inflater.inflate(R.layout.fragment_registration_survey, container, false);		

		mFinishSurveyButton = (Button) root.findViewById(R.id.finishSurveyButton);
		mFinishSurveyButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finishSurvey();
			}
		});
		return root;
	}

	

	private Fragment createNestedFragment(ArrayList<Question> questions) {
		Fragment fragment = new QuestionsFragment();
		Bundle args = new Bundle();
		args.putSerializable("questions", questions);
		fragment.setArguments(args);
		return fragment;
	}

	private void showNestedFragment(Fragment fragment) {
		FragmentTransaction ft = getChildFragmentManager().beginTransaction();
		ft.replace(R.id.fragment, fragment);
		ft.addToBackStack(null);
		ft.commit();
	}

	/**
	 * Checks every time an answer changes if all questions are answered and, if yes, enables the finish button.
	 */
	@Override
	public void onAnswerChanged() {

		if (mRegistrationSurvey.areCurrentQuestionsAnswered()) {
			mFinishSurveyButton.setEnabled(true);
		} else {
			mFinishSurveyButton.setEnabled(false);
		}

	}

	private void finishSurvey() {
		// Send the finished survey back to the enclosing Activity
		// (=StartActivity) and close the dialog window.
		RegistrationSurveyListener activity = (RegistrationSurveyListener) getActivity();
		activity.onFinishRegistrationSurvey(mRegistrationSurvey);
		RegistrationSurveyFragment.this.dismiss();
	}
}
